package cn.kgc.crowdfundingadminspringboot2.controller;

import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.pojo.Cert;
import cn.kgc.crowdfundingadminspringboot2.service.CertService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/cert")
public class CertController {
    @Resource
    CertService certService;

    @RequestMapping("/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<Cert>> searchBYkeyword(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize,
                                                        @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword) {
        PageInfo<Cert> certPageInfo = certService.queryForkeywordPage(pageNum, pageSize, keyword);

        ResultEntity<PageInfo<Cert>> resultEntity = ResultEntity.successWithoutData(certPageInfo);
        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/save/cert")
    public ResultEntity<String> saveRole(@RequestParam("certName") String certName) {

        certService.saveCert(certName);

        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> certIdList) {
        certService.batchRemove(certIdList);
        return ResultEntity.successWithoutData();

    }

    @ResponseBody
    @RequestMapping("/get/list/by/id/list")
    public ResultEntity<List<Cert>> getRoleListByIdList(@RequestBody List<Integer> certIdList) {
        List<Cert> certListByIdList = certService.getRoleListByIdList(certIdList);
        return ResultEntity.successWithoutData(certListByIdList);
    }
    @ResponseBody
    @RequestMapping("/update/cert")
    public ResultEntity<String> updatecert(Cert cert){
        certService.updateRole(cert);
     return    ResultEntity.successWithoutData();
    }
}
